﻿package cookinmaddy.web;


/**
 * ...
 * @author Yog
 */

 import cookinmaddy.model.Recipe;
 import cookinmaddy.database.DBManager;
 import cookinmaddy.database.spod.DBRecipe;
 import cookinmaddy.web.CookinMaddyApp;
 import cookinmaddy.templo.TemploManager;
 import php.Lib;
 import php.Session;
 
class PHPMain 
{
	private static var DATABASE : String = "database/db.db3";
	private static var TRACEFOLDER : String = "traces";
	private static var TIMESTAMP : String ;
	static function main() 
	{
		Session.start();
		initTraces();
		
/*		DBManager.initializeDB(DATABASE);
		var rec : Recipe = new Recipe();
		rec.Name = "titi";
		var db : DBRecipe = new DBRecipe();
		db.value = rec;
		try
		{
			db.insert();
		}
		catch (exc : String)
		{
			trace(exc);
		}*/
		
		trace("- - - - - - - - - - - - - - - - - - - - - - - -");
		trace("BEGIN");
		try
		{
			trace("Session : " + Session.getId() + "");

			var params = php.Web.getParams();
			trace("page parameters: " + params );
			var action : String = params.get("action");
			trace("action: " + action + "");
			trace("data: " + params.get("data") + "");
			
			if (action == "createdb")
			{
				try
				{
					trace("Initialize Database");
					DBManager.createDB(DATABASE);
				}
				catch (e : String)
				{
					trace(e);
				}
				action = null;
			}
			else
			{
				trace("Init database ");
				DBManager.initializeDB(DATABASE);
			}
			trace("Init templo ");
			TemploManager.initializeTemplo();
			
			trace("retrieve _app from session ");			
			if (Session.exists("_app"))
			{
				trace("_app found ");
				CookinMaddyApp._app = Session.get("_app");	
			}
			else
			{
				trace("_app not found ");
			}
			var data = "";
			if (params.exists("data"))
			{
				data = params.get("data");
			}
			trace("switch action: " + action);
			switch(action)
			{
				case "listrecipes":
					trace("case: listrecipes");
					CookinMaddyApp.listrecipes();
				case "addrecipe":
					trace("case: addrecipe");
					CookinMaddyApp.addrecipe(data);					
				case "listunits":
					trace("case: listunits");
					CookinMaddyApp.listunits();					
				case "updateunit":
					trace("case: listunits");
					CookinMaddyApp.updateunit(data);		
				case "addunit":
					trace("case: listunits");
					CookinMaddyApp.addunit(data);					
				case "delunit":
					CookinMaddyApp.delunit(data);
				case "listingredients":
					trace("case: listunits");
					CookinMaddyApp.listingredients();
				case "updateingredient":
					trace("case: listunits");
					CookinMaddyApp.updateingredient(data);		
				case "addingredient":
					trace("case: addingredient");
					CookinMaddyApp.addingredient(data);					
				case "delingredient":
					CookinMaddyApp.delingredient(data);					
				case null:
					trace("case: null");
					Session.clear();
					initialize();
					CookinMaddyApp.home();
				case "":
					trace("case: \"\"");
					Session.clear();
					initialize();
					CookinMaddyApp.home();
				default:
					trace("case: default");
					Lib.print("Unknown action: " + action);
			}
		}
		catch (e : String)
		{
			trace("ERROR: " + e + "");
		}
		trace("END");
		
	}
	
	private static function initialize()
	{
		trace("BEGIN");
		Session.set("_app", CookinMaddyApp.initialize());
		trace("END");
	} 
	static var TRACEFILENAME : String = "";
	public static function initTraces()
	{
		TIMESTAMP = Date.now().getTime() + "" ;
		TRACEFILENAME = TRACEFOLDER + "/" + Session.getId() + "-traces" + TIMESTAMP + ".txt";
		haxe.Log.trace = myTrace;		
	}
	public static function myTrace( v : Dynamic, ?pos : haxe.PosInfos ) : Void
	{
		var stream = php.io.File.append( TRACEFILENAME, false );
		var str = pos.fileName + "(" + pos.lineNumber + ") - " + pos.className + "." + pos.methodName + " : " + v + "\n";	
		try
		{
			stream.writeString( str );
			stream.flush();
		}
		catch ( err : Dynamic)
		{
			php.Lib.print( err );
		}
		stream.close();	  
	}	
	
	public static function htmlinitTraces()
	{
		haxe.Log.trace = myhtmlTrace;
	}
	public static function myhtmlTrace( v : Dynamic, ?pos : haxe.PosInfos ) : Void
	{
		var str = pos.fileName + "(" + pos.lineNumber + ") - " + pos.className + "." + pos.methodName + " : " + v + "<br>";	
		php.Lib.print( str );
	}
}